#include "startprogram.h"
#include "erfen.h"
#include "solve_dfs.h"
#include "solve_bfs.h"
#include "solve_astar.h"
#include "Cmd_Draw.h"
#include "map.h"

using namespace std;

int map[1000][1000];
int row,col;
Point startpoint,endpoint;
int erfennum;
int dfsnum;
int bfsnum; 
int astarnum;

#define TIMES 10

void InitMap()
{
	Set_Color(6);
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<col;j++)
		if(map[i][j]==0) cout<<"  ";
		else cout<<"##";
		cout<<"\n";
	}

}

void InitAll()
{
	
	////file input mode
	// if(!freopen("C:\\Users\\Simon\\Desktop\\map5151.txt","r",stdin)) cout<<"Input File Not Found!"<<endl;
	// cin >> row >> col;
	// for (int i = 0; i < row; i++) {
	// 	for (int j = 0; j < col ;j++) {
	// 		cin >> map[i][j];
	// 	}
	// }

	////**UPDATE** now we can produce the map ramdonly 
	cout<<"Welcome! Please type in your favourite row and col number of the maze and adjust the terminal screen to full-screen."<<endl;
	cin >> row >> col;
	cout<<"Good! Initializing the map and please wait."<<endl;
	if(row%2 == 0) row+=1;
	if(col%2 == 0) col+=1; // keep row and col odd
	start_mapping();

	Sleep(1000);
	Clear_Grapics();
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<col;j++)
		{
			cout<<map[i][j]<<" ";
		}
		cout<<endl;
	}

	startpoint.x=1;
	startpoint.y=1;
	endpoint.x=row-2;
	endpoint.y=col-2;

	cout<<"READY? LET'S GO!"<<endl;
	Sleep(1000);
}


void SetRunningTime(int time)
{
	time = clock();
}

int main()
{
	InitAll();
	
	clock_t starttime, endtime;
	double totaltime_of_dfs=0,totaltime_of_bfs=0,totaltime_of_erfen=0,totaltime_of_astar=0;

	//for demonstrating
	Clear_Grapics();InitMap();
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<"DFS   Method";
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<endl;
	solve_dfs(startpoint.x,startpoint.y);
	Sleep(1000);
	cout<<endl;print_dfs_ans();
	Sleep(3000);


	Clear_Grapics();InitMap();
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<"BFS   Method";
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<endl;
	solve_bfs(startpoint.x,startpoint.y);
	print_bfs_path(endpoint.x,endpoint.y);
	Sleep(1000);
	cout<<endl;print_bfs_ans(endpoint.x,endpoint.y);
	Sleep(3000);


	Clear_Grapics();InitMap();
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<"ERFEN Method";
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<endl;
	init_erfen(); 
	entrance r;
	exit1 ch;
	r.push(1,1);
	ch.push(row-2,col-2);
	move(ch,r);
	Sleep(3000);


	Clear_Grapics();InitMap();
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<"ASTAR Method";
	for(int i=0;i<(row-6);i++) cout<<"-";
	cout<<endl;
	solve_astar(startpoint, endpoint);
	print_astar_path(endpoint.x, endpoint.y);
	Sleep(1000);
	cout<<endl;print_astar_ans(endpoint.x, endpoint.y);
	Sleep(3000);




	//for calculating running time and efficiency
	// for(int i=0;i<TIMES;i++)
	// {

	// 	dfsnum=0;
	// 	Clear_Grapics();InitMap();
	// 	starttime = clock();
	// 	solve_dfs(startpoint.x,startpoint.y);
	// 	print_dfs_ans();
	// 	endtime = clock();
	// 	totaltime_of_dfs += (double)( (endtime - starttime)/(double)CLOCKS_PER_SEC);

	// 	Sleep(1000);

	// 	bfsnum=0;
	// 	Clear_Grapics();InitMap();
	// 	starttime = clock();
	// 	solve_bfs(startpoint.x,startpoint.y);
	// 	print_bfs_path(endpoint.x,endpoint.y);
	// 	print_bfs_ans(endpoint.x,endpoint.y);
	// 	endtime = clock();
	// 	totaltime_of_bfs += (double)( (endtime - starttime)/(double)CLOCKS_PER_SEC);

	// 	Sleep(1000);

	// 	erfennum=0;
	// 	Clear_Grapics();InitMap();
	// 	starttime = clock();
	// 	init_erfen(); 
	// 	ru r;
	// 	chu ch;
	// 	r.push(1,1);
	// 	ch.push(row-2,col-2);
	// 	move(ch,r);
	// 	endtime = clock();
	// 	totaltime_of_erfen += (double)( (endtime - starttime)/(double)CLOCKS_PER_SEC);

	// 	Sleep(1000);

	// 	Clear_Grapics();InitMap();
	// 	starttime = clock();
	// 	solve_astar(startpoint, endpoint);
	// 	print_astar_path(endpoint.x, endpoint.y);
	// 	print_astar_ans(endpoint.x, endpoint.y);
	// 	endtime = clock();
	// 	totaltime_of_astar += (double)( (endtime - starttime)/(double)CLOCKS_PER_SEC);
	// }

	// Clear_Grapics();
	// cout<<"Total running time of Erfen method is "<<totaltime_of_erfen/TIMES<<"\n";
	// cout<<"Total running time of DFS   method is "<<totaltime_of_dfs  /TIMES<<"\n";
	// cout<<"Total running time of BFS   method is "<<totaltime_of_bfs  /TIMES<<"\n";
	// cout<<"Total running time of ASTAR method is "<<totaltime_of_astar/TIMES<<"\n";

	Clear_Grapics();
	cout<<"the number of cycles of Erfen method is "<<erfennum<<"\n";
	cout<<"the number of cycles of DFS   method is "<<dfsnum  <<"\n";
	cout<<"the number of cycles of BFS   method is "<<bfsnum  <<"\n";
	cout<<"the number of cycles of ASTAR method is "<<astarnum<<"\n";

	Sleep(3000000);
	
	return 0;
}
